package cn.iocoder.yudao.module.shop.admin.mq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.api.infrastructure.user.AdminUserApiV2;
import cn.iocoder.yudao.module.api.member.user.message.MemberUserDeleteMessage;
import cn.iocoder.yudao.module.shop.admin.pojo.po.ShopUser;
import cn.iocoder.yudao.module.shop.admin.service.ShopUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户注销 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class AdminDeleteConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(MemberUserDeleteMessage message) {
        log.info("[shop][onMessage][用户删除事件({})]", message);
        List<ShopUser> list = SpringUtil.getBean(ShopUserService.class).getAdminListByUserId(message.getUserId(), UserTypeEnum.MEMBER.getValue());
        if (CollUtil.isNotEmpty(list)) {
            AdminUserApiV2 adminUserApiV2 = SpringUtil.getBean(AdminUserApiV2.class);
            for (ShopUser admin : list) {
                adminUserApiV2.deleteById(admin.getAdminUserId());
            }
        }
    }

}
